package com.hhit.bigscreenbackmanage.service.impl.gis;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.dao.gis.TGisSituationDAO;
import com.hhit.bigscreenbackmanage.entity.gis.TGisSituation;
import com.hhit.bigscreenbackmanage.entity.tgygl.TgyglFileEntity;
import com.hhit.bigscreenbackmanage.entity.tgygl.TgyglWithStateEntity;
import com.hhit.bigscreenbackmanage.service.gis.TGisSituationService;
import com.hhit.bigscreenbackmanage.util.Utils;
import com.hhit.bigscreenbackmanage.vo.gis.GisAqglVO;
import com.hhit.bigscreenbackmanage.vo.gis.GisGcVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hhit.bigscreenbackmanage.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhl
 */
@Service
public class TGisSituationServiceImpl implements TGisSituationService {

    private Logger log = LoggerFactory.getLogger(TGisSituationServiceImpl.class);

    @Autowired
    private TGisSituationDAO tGisSituationDAO;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(TGisSituation record) {
        return 0;
    }

    @Override
    public int insertSelective(TGisSituation record) {
        return 0;
    }

    @Override
    public TGisSituation selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TGisSituation record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TGisSituation record) {
        return 0;
    }

    @Override
    public Map<String, Object> insertFromExcel(MultipartFile[] file,
                                               String cityName,
                                               String propName) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelDataForGis(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TGisSituation> list = (List<TGisSituation>) Utils.mapToEntiry(result, TGisSituation.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TGisSituation tGisSituation : list) {
                        // tGisSituation.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tGisSituation.setJgdw(cityName);
                    }

                    tGisSituationDAO.insertFromExcel(list);
                }
            }
            resultMap.put("flag", true);
        } catch (Exception e) {
            resultMap.put("flag", false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    public class CoordDms
         {
                     public int Degree;
                     public int Minute;
                     public Double Second;

             public int getDegree() {
                 return Degree;
             }

             public void setDegree(int degree) {
                 Degree = degree;
             }

             public int getMinute() {
                 return Minute;
             }

             public void setMinute(int minute) {
                 Minute = minute;
             }

             public Double getSecond() {
                 return Second;
             }

             public void setSecond(Double second) {
                 Second = second;
             }
         }

    /**
     * 度分秒转经纬度
     * @param dms 116°25'7.85"
     * @return 116.418847
     */
    public static double changeToDu(String dms) {
        if (dms == null) return 0;
        try {
            dms = dms.replace(" ", "");
            String[] str2 = dms.split("°");
            if (str2.length < 2) return 0;
            int d = Integer.parseInt(str2[0]);
            String[] str3 = str2[1].split("\'");
            if (str3.length < 2) return 0;
            int f = Integer.parseInt(str3[0]);
            String str4 = str3[1].substring(0, str3[1].length() - 2);
            double m = Double.parseDouble(str4);

            double fen = f + (m / 60);
            double du = (fen / 60) + Math.abs(d);
            if (d < 0) du = -du;
            return du;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Map<String, Object> insertFromExcelDfm(MultipartFile[] file,
                                               String cityName,
                                               String propName) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelDataForGis(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TGisSituation> list = (List<TGisSituation>) Utils.mapToEntiry(result, TGisSituation.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TGisSituation tGisSituation : list) {
                        // tGisSituation.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tGisSituation.setJgdw(cityName);
                        tGisSituation.setAwzbjd(String.valueOf(changeToDu(tGisSituation.getAwzbjd())));
                        tGisSituation.setAwzbwd(String.valueOf(changeToDu(tGisSituation.getAwzbwd())));
                    }

                    System.out.println(list);

                    tGisSituationDAO.insertFromExcel(list);
                }
            }
            resultMap.put("flag", true);
        } catch (Exception e) {
            resultMap.put("flag", false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public List<GisAqglVO> getTGisDataByTgy(JSONObject jsonObject) {

        ArrayList<GisAqglVO> gisAqglVOS1 = new ArrayList<>();

        //先查询所有的地市
        // List<String> ds = tGisSituationDAO.getAllds();
        List<String> ds = new ArrayList<String>();
        ds.add("特高压");


        //总树
        GisAqglVO gisAqglVO = null;
        //工程树
//        GisAqglVO gisAqglVOgc;
        if (ds.isEmpty()) {
            return null;
        }
        for (String d : ds) {
            gisAqglVO = new GisAqglVO();
            //地市
            gisAqglVO.setLabel(d);
            gisAqglVO.setId(ds.indexOf(d));
            //根据地市查询工程
            List<String> gcs = tGisSituationDAO.getAllgcByds(d);
            //查询数据
            List gisAqglVOS = new ArrayList<>();
            for (String gc : gcs) {
                //第二层
                GisGcVO gisAqglVOgc = new GisGcVO();
                gisAqglVOgc.setLabel(gc);
                List<TgyglWithStateEntity> datas = tGisSituationDAO.getAllDataByTgy(gc);
                if (!datas.isEmpty()) {
                    gisAqglVOgc.setChildren(datas);
                }
                gisAqglVOS.add(gisAqglVOgc);
            }
            gisAqglVO.setChildren(gisAqglVOS);
            gisAqglVOS1.add(gisAqglVO);
        }
        return gisAqglVOS1;
    }

    @Override
    public List<GisAqglVO> getTGisDataByds(JSONObject jsonObject) {

        ArrayList<GisAqglVO> gisAqglVOS1 = new ArrayList<>();

        //先查询所有的地市
        // List<String> ds = tGisSituationDAO.getAllds();
        List<String> ds = new ArrayList<String>();
        ds.add("建设分公司");
        ds.add("南京");
        ds.add("苏州");
        ds.add("无锡");
        ds.add("徐州");
        ds.add("常州");
        ds.add("镇江");
        ds.add("扬州");
        ds.add("泰州");
        ds.add("南通");
        ds.add("盐城");
        ds.add("淮安");
        ds.add("宿迁");
        ds.add("连云港");


        //根据每个地市查询下面的工程
        //总树
        GisAqglVO gisAqglVO = null;
        //工程树
//        GisAqglVO gisAqglVOgc;
        if (ds.isEmpty()) {
            return null;
        }
        for (String d : ds) {
            gisAqglVO = new GisAqglVO();
            //地市
            gisAqglVO.setLabel(d=="建设分公司"?d:"国网"+d+"供电公司");
            gisAqglVO.setId(ds.indexOf(d));
            //根据地市查询工程
            List<String> gcs = tGisSituationDAO.getAllgcByds(d);
            //查询数据
            List gisAqglVOS = new ArrayList<>();
            for (String gc : gcs) {
                //第二层
                GisGcVO gisAqglVOgc = new GisGcVO();
                gisAqglVOgc.setLabel(gc);
                List<TgyglWithStateEntity> datas = tGisSituationDAO.getAllDataBygc(gc);
                if (!datas.isEmpty()) {
                    gisAqglVOgc.setChildren(datas);
                }
                gisAqglVOS.add(gisAqglVOgc);
            }
            gisAqglVO.setChildren(gisAqglVOS);
            gisAqglVOS1.add(gisAqglVO);
        }
        return gisAqglVOS1;
    }

    @Override
    public Map<String, Object> queryTGisFileData(JSONObject jsonObject) {
        Map<String, Object> map = new HashMap<>();
        //先查询总条数
        //获取查询条件
        PageUtil page = new PageUtil();
        int count = tGisSituationDAO.queryAllCount(jsonObject);
        // 每页数
        page.pageCount = Integer.parseInt(jsonObject.get("pageSize").toString());
        //参数1：总行数，参数2：当前页
        page.init(count, Integer.parseInt(jsonObject.get("pageNo").toString()));
        jsonObject.put("rowNum", page.getRowNum());
        jsonObject.put("pageCount", page.getPageCount());
        List<TGisSituation> tGisSituations = tGisSituationDAO.queryAl(jsonObject);
        map.put("page", page);
        map.put("list", tGisSituations);

        return map;
    }


}
